﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.RobotCommunication.Enum;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.RobotCommunication
{
    public class Message
    {
        //MessageType
        public MessageType msgType
        {
            get;
            set;
        }

        //MessageID
        public MessageID nMsgID
        {
            get;
            set;
        }

        /// <summary>
        /// 消息名称
        /// </summary>
        public String strMsgName
        {
            get;
            set;
        }

        /// <summary>
        /// 消息参数
        /// </summary>
        private IDictionary<String, Object> ArgsDict = new Dictionary<String, Object>();

        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="arg"></param>
        public void AddArg(String strKey, Object value)
        {
            this.ArgsDict.Add(strKey, value);
        }

        /// <summary>
        /// 删除参数
        /// </summary>
        /// <param name="arg"></param>
        public void DeleteArg(String strKey)
        {
            this.ArgsDict.Remove(strKey);
        }

        /// <summary>
        /// 删除参数
        /// </summary>
        /// <param name="arg"></param>
        public void DeleteAllArg()
        {
            this.ArgsDict.Clear();
        }

        /// <summary>
        /// 取得参数
        /// </summary>
        /// <param name="arg"></param>
        public Object GetArg(String strKey)
        {
            Object value;
            if (this.ArgsDict.TryGetValue(strKey, out value))
                return value;
            else
                return null;
        }

        /// <summary>
        /// 取得Keys
        /// </summary>
        /// <param name="arg"></param>
        public IList<String> GetArgKeys()
        {
            IList<String> keyList = new List<String>();
            Int32 nCnt = this.ArgsDict.Count();
            for (Int32 nIndex = 0; nIndex < nCnt; nIndex++)
                keyList.Add(this.ArgsDict.Keys.ElementAt(nIndex));

            return keyList;
        }

        /// <summary>
        /// 取得元素数
        /// </summary>
        /// <param name="arg"></param>
        public Int32 GetArgCount()
        {
            return this.ArgsDict.Count();
        }

        /// <summary>
        /// 转换成Json串
        /// </summary>
        /// <returns></returns>
        public String ToJson()
        {
            IList<ArgJson> argList = new List<ArgJson>();

            foreach (var item in this.ArgsDict)
            {
                ArgJson arg = new ArgJson();
                arg.name = item.Key.ToString();
                arg.value = item.Value;

                if (item.Value is Int32)
                    arg.type = ArgType.Int;
                else if (item.Value is Double)
                    arg.type = ArgType.Double;
                else if (item.Value is String)
                    arg.type = ArgType.String;
                else
                    arg.type = ArgType.Object;

                argList.Add(arg);
            }

            var formatMsg = new
            {
                ID = this.nMsgID,
                Type = this.msgType,
                Name = this.strMsgName,
                Args = argList
            };

            String strJson = JsonConvert.SerializeObject(formatMsg);

            return strJson;
        }
    }

    public class ArgJson
    {
        public ArgType type
        {
            get;
            set;
        }

        public Object value
        {
            get;
            set;
        }

        public String name
        {
            get;
            set;
        }
    }
}
